/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.view.component;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author yura
 */
public class QuestionDialog {

    /**
     *  Show user message box with question and list of chose
     * @param title title of the message box
     * @param message the message that user will se
     * @param options the button options
     * @param messageOptions list of the user choses
     * @return 0 if user cancel the chose and chosen number if he chose something
     */
    public static int question(String title, String message, String options[], 
            String... messageOptions){

        JPanel panel = new JPanel(new BorderLayout());
        Box box = Box.createVerticalBox();
        ButtonGroup bg = new ButtonGroup();
        int iterator = 0;
        List<ButtonModel> choise = new ArrayList<ButtonModel>();
        for (String string : messageOptions) {
            JRadioButton rb = new JRadioButton(string);
            bg.add(rb);
            box.add(rb);
            box.add(Box.createHorizontalStrut(5));

            if (iterator ++ == 0){
                rb.setSelected(true);
            }
            choise.add(rb.getModel());
        }
        panel.add(box);
        
        int i = JOptionPane.showOptionDialog(null, panel, title,
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, options[0]);

        if (i == JOptionPane.OK_OPTION){
            return choise.indexOf(bg.getSelection()) + 1;
        }

        return 0;
    }

}
